package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Discuss;
import cn.sunlelin.educationserver.entity.Discussreply;
import cn.sunlelin.educationserver.service.DiscussreplyService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class DiscussreplyController {
    @Autowired
    private DiscussreplyService discussreplyService;

    @PostMapping("/discussreply/createDiscussreply")
    public Map<String,Object> createDiscuss(@RequestBody Discussreply discussreply){
        Map<String,Object> map=new HashMap<>();
        if(discussreplyService.createDiscussreply(discussreply)){
            map.put("code",200);
            map.put("msg","回复讨论成功");
            map.put("dataobject",null);
        }
        return map;
    }
    @GetMapping("/discussreply/selectDiscussreplyByUserId/{userId}")
    public Map<String,Object> selectDiscussreplyByUserId(@PathVariable Integer userId){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> discussreplyList = discussreplyService.selectDiscussreplyByUserId(userId);
        if(discussreplyList.size()!=0){
            map.put("code",200);
            map.put("msg","用户Id查询回复成功");
            map.put("dataobject",discussreplyList);
        }
        else {
            map.put("code",500);
            map.put("msg","用户Id查询回复失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/discussreply/getPageByUserId/{currentPage}/{pageSize}/{userId}")
    public Map<String,Object> getPageByUserId(@PathVariable Integer currentPage,
                                                 @PathVariable Integer pageSize,
                                                 @PathVariable Integer userId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        System.out.println("userId==="+userId);
        Page<Map<String,Object>> page= discussreplyService.getPageByUserId(currentPage,pageSize,userId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","用户Id分页查询回复成功");
        map.put("dataobject",m);
        return map;
    }

    @GetMapping("/discussreply/selectDiscussreplyByDiscussId/{discussId}")
    public Map<String,Object> selectDiscussreplyByDiscussId(@PathVariable Integer discussId){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> discussreplyList = discussreplyService.selectDiscussreplyByDiscussId(discussId);
        if(discussreplyList.size()!=0){
            map.put("code",200);
            map.put("msg","讨论Id查询回复成功");
            map.put("dataobject",discussreplyList);
        }
        else {
            map.put("code",500);
            map.put("msg","讨论Id查询回复失败");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/discussreply/getPageByDiscussId/{currentPage}/{pageSize}/{discussId}")
    public Map<String,Object> getPageByDiscussId(@PathVariable Integer currentPage,
                                                @PathVariable Integer pageSize,
                                                @PathVariable Integer discussId){
        if(currentPage==null) currentPage=1;
        if(pageSize==null)pageSize=2;
        Page<Map<String,Object>> page= discussreplyService.getPageByDiscussId(currentPage,pageSize,discussId);
        List<Map<String,Object>> pageList=page.getResult();
        long total = page.getTotal();
        Map<String,Object> m=new HashMap<>();
        m.put("pageList",pageList);
        m.put("total",total);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        map.put("msg","讨论Id分页查询回复成功");
        map.put("dataobject",m);
        return map;
    }

    @GetMapping("/discussreply/deleteDiscussreply/{discussreplyId}")
    public Map<String,Object> deleteDiscussreply(@PathVariable Integer discussreplyId){
        Map<String,Object> map=new HashMap<>();
        if(discussreplyService.deleteDiscussreply(discussreplyId)){
            map.put("code",200);
            map.put("msg","删除回复成功");
            map.put("dataobject",null);
        }
        else {
            map.put("code",500);
            map.put("msg","删除回复失败");
            map.put("dataobject",null);
        }
        return map;
    }
}
